<?php

namespace App\Module\Admin\Controllers;

use App\System\Coupon\Repositories\CouponUserRepository;
use App\System\Fina\Repositories\FinaIntegralRegRecordRepository;
use Illuminate\Http\Request;

/**
 * Class FinanceController 财务管理
 * @package App\Module\Admin\Controllers
 */
class FinanceController extends Controller
{
    /**
     * 财务管理
     */

    //创客财务
    public function index(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $keyword = $request['keyword']??'';
        $recond_type = $request['record_type']??'';
        $status = $request['status']??'';
        $page = $request['page']??1;
        $where = array('start_time'=>$startTime,'end_time'=>$endTime,'keyword'=>$keyword,'record_type'=>$recond_type,'status'=>$status,'page'=>$page);
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist($recond_type,$status,$keyword,$startTime,$endTime);
        $typeArr = ['1'=>'充值','2'=>'订单','3'=>'注册币转入','4'=>'注册币转出','5'=>'报单','6'=>'日工资','7'=>'提现','8'=>'管理奖','9'=>'企业推荐奖','10'=>'积分转注册币','11'=>'资金调节','12'=>'服务奖励',13=>'复消'];
        $statusArr = ['1'=>'成功','2'=>'失败','3'=>'待审核'];
        return view('admin.finance.index',compact('data','where','typeArr','statusArr'));
    }

    // 导出财务列表
    public function downList(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $keyword = $request['keyword']??'';
        $recond_type = $request['record_type']??'';
        $status = $request['status']??'';
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist($recond_type,$status,$keyword,$startTime,$endTime,'','',true);
        $typeArr = ['1'=>'充值','2'=>'订单','3'=>'注册币转入','4'=>'注册币转出','5'=>'报单','6'=>'日工资','7'=>'提现','8'=>'管理奖','9'=>'企业推荐奖','10'=>'积分转注册币','11'=>'资金调节','12'=>'服务奖励','13'=>'复消'];
        $statusArr = ['1'=>'成功','2'=>'失败','3'=>'待审核'];
        $arr['headArr'] = array('时间','创客昵称','手机号码','流水号','变动类型','财务类型','交易方式','变动金额','交易结果','变动后金额');
        foreach ($data as $v) {
            $thearr = array();
            $thearr['created_at'] = $v->created_at;
            $thearr['name'] = $v->user['name'];
            $thearr['mobile'] = $v->user['mobile'];
            $thearr['trade_no'] = $v->trade_no;
            $thearr['record_type'] = ($v->record_type==1)?'积分':'注册币';
            $thearr['income'] = ($v->income==1)?'收入':'支出';
            $thearr['type'] = $typeArr[$v->type];
            $thearr['cash'] = $v->cash;
            $thearr['status'] = $statusArr[$v->status];
            $thearr['after_cash'] = $v->after_cash;
            $arr['data'][] = $thearr;
        }
        $this->getExcel('创客财务',$arr['headArr'],$arr['data'],['cash','after_cash']);
    }

    //创客提现
    public function getCash(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $status = $request['status']??'';
        $page = $request['page']??1;
        $where = array('start_time'=>$startTime,'end_time'=>$endTime,'status'=>$status,'page'=>$page);
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist('',$status,'',$startTime,$endTime,7);
        $statusArr = ['1'=>'已转账','2'=>'转账失败','3'=>'申请中'];
        return view('admin.finance.getCash',compact('data','where','statusArr'));
    }

    //处理创客提现
    public function ajaxCheckGetCash(Request $request)
    {
        if($request->isMethod('post')){
            //获取指定字段内容
            $thepost = $request->only('ids','status');
            $ids = $thepost['ids']??'';
            $updata['status'] = $thepost['status'];
            $BaseRegion = FinaIntegralRegRecordRepository::getInstance();
            if(is_array($ids)){
                $res = $BaseRegion->editDatas($ids,$updata);
            }else{
                $res = $BaseRegion->editData($ids,$updata);
            }
            $msg = $res['message']??'操作失败';
            $state = !empty($res['data'])?'SUCCESS':'FAIL';
            return response()->json(['message' => $msg, 'status' => $state,'url'=>'']);
        }
    }

    //充值记录
    public function rechargeLog(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $pay_type = $request['pay_type']??'';
        $page = $request['page']??1;
        $keyword = $request['keyword']??'';
        $where = array('start_time'=>$startTime,'end_time'=>$endTime,'pay_type'=>$pay_type,'page'=>$page,'keyword'=>$keyword);
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist('','',$keyword,$startTime,$endTime,1,$pay_type);
        $statusArr = ['1'=>'成功','2'=>'失败','3'=>'待审核'];
        return view('admin.finance.rechargeLog',compact('data','where','statusArr'));
    }

    // 导出充值记录列表
    public function downRechargeLogList(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $pay_type = $request['pay_type']??'';
        $keyword = $request['keyword']??'';
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist('','',$keyword,$startTime,$endTime,1,$pay_type,true);
        $statusArr = ['1'=>'成功','2'=>'失败','3'=>'待审核'];
        $arr['headArr'] = array('时间','创客昵称','手机号码','平台流水号','第三方流水号','变动类型','支付方式','充值金额','交易结果','变动后金额');
        foreach ($data as $v) {
            $thearr = array();
            $thearr['created_at'] = $v->created_at;
            $thearr['mobile'] = $v->user['mobile'];
            $thearr['trade_no'] = $v->pay['trade_no'];
            $thearr['payment_order'] = $v->pay['payment_order'];
            $thearr['record_type'] = ($v->record_type==1)?'积分':'注册币';
            $thearr['payment_type'] = ($v->pay['payment_type']==1)?'微信':'支付宝';
            $thearr['cash'] = $v->cash;
            $thearr['status'] = $statusArr[$v->status];
            $thearr['after_cash'] = $v->after_cash;
            $arr['data'][] = $thearr;
        }
        $this->getExcel('充值记录',$arr['headArr'],$arr['data'],['cash','after_cash']);
    }


    //待结算积分
    public function integral(Request $request)
    {
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $status = $request['status']??'';
        $page = $request['page']??1;
        $keyword = $request['keyword']??'';
        $where = array('start_time'=>$startTime,'end_time'=>$endTime,'status'=>$status,'page'=>$page,'keyword'=>$keyword);
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist(1,$status,$keyword,$startTime,$endTime,6,'','','',1);
        $statusArr = ['1'=>'已发放','2'=>'不发放','3'=>'正常发放'];
        return view('admin.finance.integral',compact('data','where','statusArr'));
    }

    //待结算注册币
    public function registerCoin(Request $request)
    {
        $keyword = $request['keyword']??'';
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $status = $request['status']??'';
        $page = $request['page']??'';
        $where = ['start_time'=>$startTime,'end_time'=>$endTime,'keyword'=>$keyword,'status'=>$status,'page'=>$page];
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist(2,$status,$keyword,$startTime,$endTime,6,'','','',1);
        $statusArr = ['1'=>'已发放','2'=>'不发放','3'=>'正常发放'];
        return view('admin.finance.registerCoin',compact('data','where','statusArr'));
    }

    //处理待结算积分和注册币
    public function ajaxInteCoin(Request $request)
    {
        if($request->isMethod('post')){
            //获取指定字段内容
            $thepost = $request->only('ids','status','remark','type');
            $ids = $thepost['ids']??'';
            $updata['status'] = $thepost['status'];
            $updata['reasons'] = $thepost['remark'];
            $BaseRegion = FinaIntegralRegRecordRepository::getInstance();
            if(is_array($ids)){
                $res = $BaseRegion->editDatas($ids,$updata);
            }else{
                $res = $BaseRegion->editData($ids,$updata);
            }
            $msg = $res['message']??'操作失败';
            $state = !empty($res['data'])?'SUCCESS':'FAIL';
            return response()->json(['message' => $msg, 'status' => $state,'url'=>'']);
        }
    }

    // 导出待结算积分、注册币列表
    public function downRegisterCoinList(Request $request)
    {
        $keyword = $request['keyword']??'';
        $startTime = $request['start_time']??'';
        $endTime = $request['end_time']??'';
        $status = $request['status']??'';
        $type = $request['type'];
        $finance = FinaIntegralRegRecordRepository::getInstance();
        $data = $finance->getlist($type,$status,$keyword,$startTime,$endTime,6,'',true,'',1);
        $statusArr = ['2'=>'不发放','3'=>'待发放'];
        $arr['headArr'] = array('发放日期','创客ID','创客昵称','手机号码','变动类型','发放数额','发放状态');
        foreach ($data as $v) {
            $thearr = array();
            $thearr['created_at'] = $v->created_at;
            $thearr['id'] = $v->user['id'];
            $thearr['name'] = $v->user['name'];
            $thearr['mobile'] = $v->user['mobile'];
            $thearr['record_type'] = ($v->record_type==1)?'积分':'注册币';
            $thearr['cash'] = $v->cash;
            $thearr['status'] = $statusArr[$v->status];
            $arr['data'][] = $thearr;
        }
        $title = ($v->record_type==1)?'待结算积分':'待结算注册币';
        $this->getExcel($title,$arr['headArr'],$arr['data'],['cash']);
    }

    //兑换券消耗
    public function voucher(Request $request)
    {
        $page = $request['page'];
        $startTime = $request['start_time'];
        $endTime = $request['end_time'];
        $status = $request['status']??0;
        $where = ['start_time'=>$startTime,'end_time'=>$endTime,'page'=>$page,'status'=>$status];
        $coupon = CouponUserRepository::getInstance();
        $data = $coupon->getList($startTime,$endTime,$status);
        return view('admin.finance.voucher',compact('data','where'));
    }
}
